package dsx.lock;

import java.util.List;

import dsx.biz.SuperReq;
import dsx.macro.ReturnValue;

public class LockReq extends SuperReq {
	int type;	// 0 表锁，1 行锁，2 表解锁，3 行解锁
	List<String> collectionKeys;
	List<String> rowKeys;
	String ownerKey;
	int lockTimeout;

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<String> getCollectionKeys() {
		return collectionKeys;
	}

	public void setCollectionKeys(List<String> collectionKeys) {
		this.collectionKeys = collectionKeys;
	}

	public List<String> getRowKeys() {
		return rowKeys;
	}

	public void setRowKeys(List<String> rowKeys) {
		this.rowKeys = rowKeys;
	}

	public String getOwnerKey() {
		return ownerKey;
	}

	public void setOwnerKey(String ownerKey) {
		this.ownerKey = ownerKey;
	}

	public int getLockTimeout() {
		return lockTimeout;
	}

	public void setLockTimeout(int lockTimeout) {
		this.lockTimeout = lockTimeout;
	}

	@Override
	public Object handle() {
		try {
			LockAnsw answ = new LockAnsw();
			boolean lockResult = false;
			switch (type) {
			case 0:
				lockResult = DustxLock.lockCollections(collectionKeys, ownerKey, lockTimeout);
				break;
			case 1:
				if (collectionKeys.size() == 1) {
					lockResult = DustxLock.lockRows(collectionKeys.get(0), rowKeys, ownerKey, lockTimeout);
				} else {
					lockResult = DustxLock.lockRows(collectionKeys, rowKeys, ownerKey, lockTimeout);
				}
				break;
			case 2:
				DustxLock.releaseCollections(collectionKeys, ownerKey);
				lockResult = true;
				break;
			case 3:
				if (collectionKeys.size() == 1) {
					DustxLock.releaseRows(collectionKeys.get(0), rowKeys, ownerKey);
				} else {
					DustxLock.releaseRows(collectionKeys, rowKeys, ownerKey);
				}
				lockResult = true;
				break;
			}
			if (lockResult)
				answ.setRet(ReturnValue.succeed);
			else 
				answ.setRet(ReturnValue.failure);
			return answ;
		} catch (Exception ex) {
			return null;
		}
	}

}
